package cn.jupiter.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.jupiter.json.ResultSetJson;

public class DbUtils {
	
	/**
	 * l�ӷ�ʽ��jdbcֱl
	 */
	public final static String JDBC_TYPE_JDBC="jdbc";
	/**
	 * l�ӷ�ʽ�����Դ
	 */
	public final static String JDBC_TYPE_DATASOURCE="datasouce";
	/**
	 * ��ݿⷽ�ԣ�֧��oracle��mysql
	 */
	public final static String ORACLE_DIALECT="oracle";
	public final static String MYSQL_DIALECT="mysql";
	/**
	 * ��ǰ���еķ��ԣ�Ĭ��oracle
	 */
	public final static String DATABASE_DIALECT="mysql";
	/**
	 * �޸����ı����Դ��ʽ��Ĭ��l�ӳ�
	 */
	public static String JDBC_TYPE=JDBC_TYPE_DATASOURCE;
	
	public final static String ORACLE_SEQUENCE="SEQ_JAVABEAN";
	/**
	 * Ĭ�ϵ�����
	 */
	public static int DEFAULT_FIRST_ROW=50;
	/**
	 * ��ݿ���Ĭ�ϵ�id
	 */
	public final static String ORACLE_ID="id";
	/**
	 * ��ı���Ĭ�ϵ�ǰ׺
	 * ���ڶ��Ƕ����select������
	 */
	public final static String TABLE_PREFIX="J_";
	static Connection con = null;
	public static Connection  getConn() throws Exception{
		//ֱl
		if(JDBC_TYPE.endsWith(JDBC_TYPE_JDBC)){
			if(con!=null){
				try {
					if(!con.isClosed()){
						return con;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			}
			try {
				//Class.forName("com.mysql.jdbc.Driver");
				Class.forName("oracle.jdbc.driver.OracleDriver");
				  //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lottery", "root", "929929");
				  con = DriverManager.getConnection(" jdbc:oracle:thin:@192.168.0.100:1521:orcl", "szreport", "szreport");
				  return con;
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return con;
		}
		//���Դ
		if(JDBC_TYPE.endsWith(JDBC_TYPE_DATASOURCE)){
			try {
				InitialContext initContext = new InitialContext();
				Context envContext  = (Context)initContext.lookup("java:/comp/env");    
				DataSource ds = (DataSource)envContext.lookup("jdbc/simpleJupiter");    
				con = ds.getConnection();    
				
				
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}    
			return con;
		}
		throw new Exception("����JDBC_TYPE����ȷ");
	}
}
